package com.start_eternity.oasissafewalk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.start_eternity.oasissafewalk.mapper.ParentMapper;
import com.start_eternity.oasissafewalk.pojo.Logg;
import com.start_eternity.oasissafewalk.pojo.PageBean;
import com.start_eternity.oasissafewalk.pojo.Parent;
import com.start_eternity.oasissafewalk.pojo.Student;
import com.start_eternity.oasissafewalk.service.LoggService;
import com.start_eternity.oasissafewalk.service.ParentService;
import com.start_eternity.oasissafewalk.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * author:ZhangT
 * Date:2024 / 03 / 14 / 9:14
 * Description:
 */
@Service
public class ParentServiceImpl implements ParentService {

    @Autowired
    private ParentMapper parentMapper;

    @Autowired
    private LoggService loggService;

    @Autowired
    private HttpServletRequest request;


    public PageBean queryPage(int current, int size) {
        //1.设置分页参数
        PageHelper.startPage(current,size);

        //2.执行查询
        List<Parent> parents = parentMapper.queryPage(current, size);
        Page<Parent> p = (Page<Parent>) parents;
        //3.封装PageBean对象
        return new PageBean(p.getTotal(),p.getResult());
    }

    @Override
    public int inserting(Parent parent) {
        int insert = parentMapper.insert(parent);

        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行了插入家长的操作,家长内容:" + parent);
        loggService.insert(logg);

        return insert;
    }

    @Override
    public int updating(Parent parent) {
        int update = parentMapper.update(parent);

        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行根据ID修改家长,家长内容:" + parent);
        loggService.insert(logg);

        return update;
    }


    @Override
    public int delById(Integer pId) {
        int rows = parentMapper.delById(pId);

        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行根据ID删除家长,删除了ID为:" + pId);
        loggService.insert(logg);

        return rows;
    }

    @Override
    public Parent queryById(Integer pId) {
        Parent parent = parentMapper.queryByPId(pId);
        return parent;
    }

    @Override
    public List<Parent> queryListByPId(Integer pId) {
        List<Parent> parents = parentMapper.queryListByPId(pId);
        return parents;
    }

    @Override
    public String selDetails(Integer pId, Integer sId) {
        String orderNo = parentMapper.selDetails(pId, sId);
        return orderNo;
    }
}
